home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 010a / fff361.zip / ROOTPATH.C < prev    next >
C/C++ Source or Header  |  1991-09-08  |  5KB  |  102 lines

  1. /*************************************************************************
  2.  * RootPath -- Convert a pathname  argument  to  root  based  cannonical *
  3.  *             form.                                                     *
  4.  * Author:     Don A. Williams                                           *
  5.  *             CompuServ - 75410,543                                     *
  6.  *             Genie     - DON-WILL                                      *
  7.  *                                                                       *
  8.  * RootPath determines the current directory,  appends the path argument *
  9.  * (which may affect which disk the current directory is  relative  to), *
  10.  * and  qualifies  "."  and  ".." references.  The result is a complete, *
  11.  * simple, path name with drive specifier.                               *
  12.  *                                                                       *
  13.  * If the relative path the user specifies  does  not  include  a  drive *
  14.  * spec., the default drive will be used as the base. (The default drive *
  15.  * will never be changed.)                                               *
  16.  *                                                                       *
  17.  *     entry:  RelativePath -- pointer to the pathname to be expanded.   *
  18.  *             FullPath -- must point to a working buffer, see warning.     *
  19.  *                                                                       *
  20.  *     exit:   FullPath -- the full path which results.                  *
  21.  *                                                                       *
  22.  *     return: A pointer to FullPath if OK, NULL if an error occurs.     *
  23.  *                                                                       *
  24.  *     calls:  getcurdir getdisk                                         *
  25.  *                                                                       *
  26.  *     warning: FullPath  must point to a working buffer large enough to *
  27.  *              hold the longest possible relative  path  argument  plus *
  28.  *              the longest possible current directory path.             *
  29.  *                                                                       *
  30.  * RootPath  was  modeled after the public domain file "rootpath.c" with *
  31.  * fairly extensive "enhancement".  The major enhancement  is  provision *
  32.  * for  relative  paths  such  as  "..\..\here"  which  MS-DOS  does NOT *
  33.  * support.  I found that such  a  construct  would  be  very  handy  in *
  34.  * several  of  my uses of UFIND so I added it.  Provision has also been *
  35.  * made to handle either of the two element name  separator  characters; *
  36.  * the '\' of MS-DOS or the '/' of Unix.  The MS-DOS facilities actually *
  37.  * recognize  either  separator  at  the programmatic level but too many *
  38.  * MS-DOS programs do NOT.                                               *
  39.  *                                                                       *
  40.  ************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45. #include <dir.h>
  46.  
  47. char           *
  48. RootPath (char *CurDir, char *RelPath, char *FullPath) {
  49.     char           *p, *p1, *p2;
  50.  
  51.     if (RelPath[0] == '\0') {
  52.         FullPath[0] = '\0';
  53.         return (NULL);
  54.         }
  55.     if (RelPath[1] == ':') {    /* Path contains drive  */
  56.         FullPath[0] = RelPath[0];
  57.         FullPath[1] = RelPath[1];
  58.         memmove(RelPath, RelPath + 2, strlen(RelPath) + 1);
  59.         }
  60.     else {
  61.         FullPath[0] = CurDir[0];
  62.         FullPath[1] = CurDir[1];
  63.         }
  64.     FullPath[2] = '\0';
  65.  
  66.     if (strlen(RelPath) == 2 && *(RelPath + 1) == ':')
  67.         strcat(RelPath, "\\");
  68.     if ((p = strchr("\\/", *RelPath)) != NULL) strcpy(FullPath + 2, RelPath);
  69.     else {
  70.         FullPath[2] = '\\';
  71.         if (FullPath[0] != CurDir[0]) {
  72.             if (getcurdir((int) (toupper(*FullPath) - '@') + 1, &FullPath[3]))
  73.                 return (NULL);
  74.             }
  75.         else {
  76.             strcpy(FullPath, CurDir);
  77.             FullPath[strlen(FullPath) - 1] = '\0';
  78.             }
  79.         p = RelPath;
  80.         while (1) {
  81.             p1 = strchr(p, '\\');
  82.             if (!strncmp(p, "..", 2)) {
  83.                 if ((p2 = strrchr(FullPath, '\\')) != NULL) *p2 = '\0';
  84.                 p = p1 + 1;
  85.                 }
  86.             else if (!strncmp(p, ".", 1)) {
  87.                 p = p1 + 1;
  88.                 break;
  89.                 };
  90.             if (p1 == NULL) break;
  91.             }
  92.         if ((strlen(FullPath) > 3) || (strlen(FullPath) == 2))
  93.             strcat(FullPath, "\\");
  94.         strcat(FullPath, p);
  95.         }
  96.  
  97.     while ((p = strchr(FullPath, '/')) != NULL) *p++ = '\\';
  98.  
  99.     if ((strlen(FullPath) == 3) && (FullPath[2] == '\\')) FullPath[2] = '\0';
  100.     return (strlwr(FullPath));
  101.     }
  102.